<?php
declare (strict_types=1);

namespace app\store\controller\user;

use app\store\controller\Controller;
use app\store\model\store\User as StoreUserModel;
use app\store\model\user\Application as Model;
use think\db\exception\DbException;

class Application extends Controller
{
    /**
     * 管理员列表
     * @return array
     * @throws DbException
     */
    public function list()
    {
        $model = new Model;
        $param = $this->request->param() ?: [];
        $studentUsermodel = new StoreUserModel;
        $param['channel_id'] = $studentUsermodel->getChildrenUserIds(!empty($param['channel_id']) ? $param['channel_id'] : $this->store['user']['store_user_id']);
        $list = $model->getList($param);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 删除
     * @param int $apiId
     * @return array|bool
     */
    public function delete(int $apiId)
    {
        // 管理员详情
        $model = Model::detail($apiId);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 新增
     * @return array
     */
    public function add()
    {
        $model = new Model;
        // 更新记录
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 更新
     * @param int $apiId
     * @return array
     */
    public function edit(int $apiId)
    {
        $model = Model::detail($apiId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 批量更新
     * @param array $apiId
     * @return array
     */
    public function batchEdit(array $apiId)
    {
        $model = new Model;
        // 更新记录
        if ($model->batchEdit($apiId, $this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    public function exportAvatar()
    {
        $model = new Model;

        $param = $this->postData() ?: [];
        $studentUsermodel = new StoreUserModel;
        $param['channel_id'] = $studentUsermodel->getChildrenUserIds(!empty($param['channel_id']) ? $param['channel_id'] : $this->store['user']['store_user_id']);

        $list = $model->exportAvatar($param);
        if (!$list) {
            return $this->renderError($model->getError());
        }
        return $this->renderSuccess($list);
    }

}
